package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.SkuSpecificationMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.vo.AttributeListIByTemplateVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements IAttributeService {
    @Autowired
    private AttributeMapper mapper;
    @Autowired
    private SkuSpecificationMapper skuSpecificationMapper;

    @Override
    public void addNew(AttributeAddNewDTO addNewDTO) {
        int count = mapper.countByNameAndTemplate(addNewDTO.getName(), addNewDTO.getTemplateId());
        if (count > 0) {
            String message = "添加属性失败，对应属性模板的属性名称已存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(addNewDTO, attribute);
        int insertRows = mapper.insert(attribute);
        if (insertRows != 1) {
            String message = "添加属性失败，服务器繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<AttributeListIByTemplateVO> listByTemplate(Long templateId) {
        int count = mapper.countByTemplateId(templateId);
        if (count < 1) {
            String message = "查询属性列表失败，没有对应属性模板的属性！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return mapper.listByTemplate(templateId);
    }

    @Override
    public AttributeStandardVO standardVO(Long id) {
        AttributeStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "查询属性详情失败，该属性不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return standardVO;
    }

    @Override
    public void deleteById(Long id) {
        AttributeStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "删除属性失败，该属性不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        } else if (skuSpecificationMapper.countByAttribute(id) > 0) {
            String message = "删除失败，该属性关联着某些sku数据！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        int deleteRows = mapper.deleteById(id);
        if (deleteRows != 1) {
            String message = "删除属性失败，服务器繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateById(Long id, AttributeUpdateDTO updateDTO) {
        AttributeStandardVO standardVO = mapper.getStandardById(id);
        if (standardVO == null) {
            String message = "修改属性失败，该属性不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int count = mapper.countByNameAndTemplateAndNotId(id, updateDTO.getName(), updateDTO.getTemplateId());
        if (count > 0) {
            String message = "修改属性失败，对应属性模板的属性名称已存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(updateDTO, attribute);
        attribute.setId(id);
        int updateRows = mapper.update(attribute);
        if (updateRows != 1) {
            String message = "更新属性失败，服务器繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }
}
